package tylerp.dnd;

public class Roll {

    /**
     * Returns the modifier used on this Roll.
     * @return an int representing the modifier
     */
    public int getModifier() {
        return modif;
    }

    /**
     * Returns the number of dice used in the Roll.
     * @return an int representing the number of dice
     */
    public int getNumDice() {
        return numDice;
    }

    /**
     * Returns the number of sides on each die used.
     * @return an int representing the number of sides
     */
    public int getNumSides() {
        return numSides;
    }

    /**
     * Returns the individual values rolled.
     * @return an array of ints that cotains the rolled values
     */
    public int[] getRolls() {
        return rolls;
    }

    /**
     * Returns the total of all rolled values plus (or minus) any modifiers.
     * @return an int representing the final total of the Roll
     */
    public int getTotal() {
        int total = 0;

        for (int i : rolls) {
            total += i;
        }

        total += modif;

        return total;
    }

    /**
     * Returns a String representing this Roll.
     * @return a String representing this Roll
     */
    @Override
    public String toString() {
        String builder = "";

        // If more than 1 die, specify how many
        if (numDice > 1) {
            builder += numDice;
        }

        // Specify what kind of die is used
        builder += "d" + numSides;

        // If there is a modifier, specify
        if (modif > 0) {
            builder += "+" + modif;
        } else if (modif < 0) {
            builder += modif;
        }

        builder += "=";

        // If more than one die is being thrown, put the rolls in brackets
        if (numDice > 1) {
            builder += "[";
        }

        // Roll numbers, and print out results
        for (int i = 0; i < numDice; i++) {
            int temp = rolls[i];
            builder += temp;
            if (i < numDice - 1) {
                builder += ",";
            }
        }

        // Close the brackets if you used them
        if (numDice > 1) {
            builder += "]";
        }

        // If you had a modifier, print it again, but update the total this time
        if (modif > 0) {
            builder += "+" + modif;
        } else if (modif < 0) {
            builder += modif;
        }

        // Show a grand total if more than one die is used or if there was a modifier
        if (numDice > 1 || modif != 0) {
            builder += "=" + getTotal();
        }

        return builder;
    }

    /**
     * The main constructor for Roll.
     * @param nD The number of dice used
     * @param nS The number of sides on each die
     * @param m The modifier for the Roll
     * @param r The rolls
     */
    public Roll(int nD, int nS, int m, int[] r) {
        numDice = nD;
        numSides = nS;
        modif = m;
        rolls = r;
    }
    private int numDice;
    private int numSides;
    private int modif;
    private int[] rolls;
}

